﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LGE;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace Thaneros
{
    public class Character : GameObject
    {
        // World atributes
        public int wHP = 100;
        int wHPLoss = -15;
        int wHPGain = 10;
        int wHPTime = 1000;
        double wHPTimer = 0;

        // Componentes
        public SpriteSheetState state;
        Punch punch;
        Cue thanatosWalkCue;

        // Atributos
        public int HP = 100;
        public float jumpHeight = 17;
        float moveSpeed = 10;

        // Aux
        public bool isTransforming { get { return state.actualState == States.TransfET || state.actualState == States.TransfTE; } }
        public bool isFacingLeft = false;
        public bool isEros = true;
        float gravityAcc = 1f; // não pode ser menor que 1!
        public Vector2 speed = new Vector2(0, 0);
        bool onGround = false;

        public Character(PlayScreen ps)
            : base(ps, null)
        {
            thanatosWalkCue = ps.soundBank.GetCue("Thanatos_Walk");
            
            thanatosWalkCue.Play();
            punch = new Punch(ps);
            position = new Vector2(500, 1200);
            drawPosition = new Vector2(500, 500);

            state = new SpriteSheetState(this);
            state.Add(Engine.Load<Texture2D>("Images\\Eros\\stand"), new SpriteSheet(this, 12, 80, true));
            state.Add(Engine.Load<Texture2D>("Images\\Eros\\transfET"), new SpriteSheet(this, 17, 40, false));
            state.Add(Engine.Load<Texture2D>("Images\\Thanatos\\stand"), new SpriteSheet(this, 8, 80, true));
            state.Add(Engine.Load<Texture2D>("Images\\Thanatos\\transfTE"), new SpriteSheet(this, 17, 40, false));
            state.Add(Engine.Load<Texture2D>("Images\\Thanatos\\attack_body"), new SpriteSheet(this, 14, 40, false));
            state.Change(States.StandThanatos);
            state.Change(States.StandEros);
            hitBox = new Rectangle(0, 0, state.spriteSheets[0].frameWidth, sprite.Height);
            origin = new Vector2(hitBox.Width / 2, hitBox.Height / 2);
        }

        public override void Update(GameTime gameTime)
        {
            UpdateHealth(gameTime);
            speed.X = 0;
            if (!isTransforming && !punch.isActive) UpdateInput(gameTime);
            UpdatePosition(gameTime);
            punch.Update(gameTime);
            state.Update(gameTime);

            if (isTransforming && state.completed) state.Change(isEros ? States.StandEros : States.StandThanatos);
        }

        private void UpdateHealth(GameTime gameTime)
        {
            wHPTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (wHPTimer > wHPTime)
            {
                wHPTimer -= wHPTime;
                wHP += isEros ? wHPGain : wHPLoss;
                wHP = (int)MathHelper.Clamp(wHP, 0, 100);
            }
        }

        private void UpdateInput(GameTime gameTime)
        {
            if (Input.KeyDown(Keys.Left))
            {
                speed.X = -moveSpeed;
                isFacingLeft = true;
                if (!isEros && thanatosWalkCue.IsPaused) thanatosWalkCue.Resume();
            }
            else if (Input.KeyDown(Keys.Right))
            {
                speed.X = moveSpeed;
                isFacingLeft = false;
                if (!isEros && thanatosWalkCue.IsPaused) thanatosWalkCue.Resume();
            }
            else
                thanatosWalkCue.Pause();

            if (Input.KeyPressed(Keys.X))
            {
                thanatosWalkCue.Pause();
                isEros = !isEros;
                state.Change(isEros ? States.TransfTE : States.TransfET);
                ps.audioEngine.SetGlobalVariable("BattleThemeVolume", isEros ? 0f : 1f);
            }
            if (Input.KeyPressed(Keys.C))
            {
                if (!isEros)
                {
                    punch.isActive = true;
                    state.Change(States.Attack);
                    ps.soundBank.PlayCue("Thanatos_Attack");
                }
                else if (onGround)
                {
                    speed.Y = -jumpHeight;
                    onGround = false;
                }
            }
        }

        public void UpdatePosition(GameTime gameTime)
        {
            speed.Y += gravityAcc; // update gravity

            // update Position.X
            if (speed.X != 0)
            {
                position.X += speed.X;
                float dH = ps.bm.CollisionH(this, speed.X < 0, true);
                if (dH != 0) position.X += dH;
                speed.X = 0;
            }

            // update Position.Y
            position.Y += speed.Y;
            float dV = ps.bm.CollisionV(this, speed.Y < 0, !onGround);
            if (dV != 0)
            {
                position.Y += dV;
                if (speed.Y > 0) onGround = true; // se estava caindo (não subindo), então agora está no chão pq colidiu (com o chão)
                speed.Y = 0;
            }
            else onGround = false;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (alive)
            {
                spriteBatch.Draw(sprite, drawPosition, sourceRectangle, Color.White, rotation, origin, scale, isFacingLeft ? SpriteEffects.FlipHorizontally : SpriteEffects.None, 0);
                punch.Draw(spriteBatch);
            }
        }


    }

    public static class States
    {
        public static int StandEros = 0;
        public static int TransfET = 1;
        public static int StandThanatos = 2;
        public static int TransfTE = 3;
        public static int Attack = 4;
    }
}
